'use strict';
// 班级管理的控制器文件：增删改查 等操作

const xtpl = require('xtpl');
const path = require('path');
const moment = require('moment');
const mysql = require('../configs/database.js');
const baseCtr = require('./baseController.js');

// 班级添加
module.exports.classAdd = function(req,res){
	xtpl.renderFile(
		path.join(__dirname,'../views/admin/class_add.html'),
		{page:'clsAdd'},
		function(err,content){
			res.setHeader('Content-Type','text/html;charset=utf-8');
			res.send(content);
		}
	)
}

module.exports.clsAddData = function(req,res){
	var where =`class_name='${req.body.cname}'`;
	mysql.table('class').field('class_id').where(where).find().then(function(result){
			if(JSON.stringify(result)=='{}'){
				var data = {
					class_name: req.body.cname,
					class_time: Date.parse(req.body.ctime)/1000,
				}

				mysql.table('class').add(data).then(function(){
					res.redirect('/admin/class/class_list');
					// console.log('添加成功');
				}).catch(function(err){
					console.log(err);
				});
			}else{
				baseCtr.sendMsg('该班级已存在',res);
			}
	}).catch(function(err){
	    console.log(err);	
	});
}

// 班级列表
module.exports.classList = function(req,res){
	var pn = req.query.pn != undefined ? req.query.pn:1;
	var limit = 3;
	var start = (pn-1)*limit;
	mysql.table('class').limit(start,limit).select().then(function(data){
		mysql.table('class').count('class_id').then(function(total){
			var pageArr = [];
			var totalPn = Math.ceil(total/limit);
			for(var i=1; i<=totalPn; i++){
				pageArr.push(i);
			}
			xtpl.renderFile(
				path.join(__dirname,'../views/admin/class_list.html'),
				{
					page:'clsList',
					clsList: data,
					pageArr: pageArr,
					pn: parseInt(pn),
					endPn: totalPn

				},
				function(err,content){
					res.setHeader('Content-Type','text/html;charset=utf-8');
					res.send(content);
				}
			)
		});	
	}).catch(function(err){
		console.log(err);
	})	
}

module.exports.clsDelData = function(req,res){
	var id = req.params.id;
	mysql.table('student').field('class_id').where(`class_id='${id}'`).find().then(function(result){
			if(JSON.stringify(result)=='{}'){
				mysql.table('student').where(`class_id='${id}'`).delete().then(function (stuRows) {
					mysql.table('class').where(`class_id='${id}'`).delete().then(function (clsRows) {
				     res.redirect('/admin/class/class_list');
					});
				});
			}else{
				baseCtr.sendMsg('该班还存在学生！',res);
			}
	})
}

module.exports.clsDelAllData = function(req,res){
	var where = `class_id in (${req.params.id})`;
	mysql.table('student').field('class_id').where(where).find().then(function(result){
			if(JSON.stringify(result)=='{}'){
				mysql.table('student').where(where).delete().then(function (stuRows) {
					mysql.table('class').where(where).delete().then(function (clsRows) {
				     res.redirect('/admin/class/class_list');
					});
				});
			}else{
				baseCtr.sendMsg('该班还存在学生！',res);
			}
	})
}

module.exports.classEdit = function(req,res){
	mysql.table('class').where({class_id:req.params.id}).find().then(function(clsData){
				// 转换时间（记得时间戳是：毫秒）
				clsData.class_time = moment(clsData.class_time*1000).format('YYYY-MM-DD');
				xtpl.renderFile(
					path.join(__dirname,'../views/admin/class_edit.html'),
					{
						page:'classEdit',
						clsList:clsData,
					},  
					function(err,content){
						res.setHeader('Content-Type','text/html;charset=utf-8');
						res.send(content);
					}
				);
		}).catch(function(err){
			console.log(err)
		})
}

module.exports.clsEditData = function(req,res){
	var where = `class_id!='${req.params.id}' and class_name='${req.body.cname}'`;
	mysql.table('class').field('class_id').where(where).find().then(function(result){
		if(JSON.stringify(result)=='{}'){
			var data = {
				class_name: req.body.cname,
				class_time: Date.parse(req.body.ctime)/1000,
			}

			mysql.table('class').where({class_id:req.params.id}).update(data).then(function(){
				console.log('修改成功')
				res.redirect('/admin/class/class_list');
			}).catch(function(err){
				console.log(err)
			})
		}else{
			baseCtr.sendMsg('该班级已存在',res);
		}
	}).catch(function(err){
		console.log(err)
	})
}
